package com.threesome.memos;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;

import com.threesome.memos.R;
import com.threesome.memos.adapter.MemosAdapter;
import com.threesome.memos.db.Memo;

public class MemoList extends Activity {
	private ImageView mNewMemoNotice;
	private Button mEditBtn;
	private Button mAddBtn;
	private ListView mList;
	private MemosAdapter mAdapter;
	private Cursor mCursor;
	private int mMemoCount;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.memo_list);
		
		mNewMemoNotice = (ImageView)findViewById(R.id.new_memo_notice);
		mEditBtn = (Button)findViewById(R.id.edit_iv);
		mAddBtn = (Button)findViewById(R.id.add_iv);		
		mList = (ListView)findViewById(R.id.memos_lv);
		
		mEditBtn.setOnClickListener(editOnClickListener);
		mAddBtn.setOnClickListener(addOnClickListener);
		
		mCursor = getContentResolver().query(Memo.Columns.CONTENT_URI, 
				null, Memo.Columns.MEMO_STATUS + "=" + Memo.MEMO_STATUS_OK, null, null);
		mAdapter = new MemosAdapter(this, mCursor);
		mList.setAdapter(mAdapter);
		mList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent(MemoList.this, AddMemo.class);
				int _id = mCursor.getInt(mCursor.getColumnIndex(Memo.Columns._ID));
				intent.putExtra(Memo.Columns._ID, _id);
				startActivity(intent);
			}			
		});
		mList.setOnItemLongClickListener(new OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {
				showDialog(DIALOG_DELETE_MEMO);
				return true;
			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
		if(mAdapter != null) {
			mMemoCount = mAdapter.getCount();
		}
		displayWhat();
	}

	private void displayWhat() {
		if(mMemoCount <= 0) {
			mNewMemoNotice.setVisibility(View.VISIBLE);
		}
		else {
			mNewMemoNotice.setVisibility(View.GONE);
		}
	}
	
	private OnClickListener editOnClickListener = new OnClickListener() {	
		public void onClick(View v) {
			startActivity(new Intent(MemoList.this, AlarmClock.class));
		}
	};
	
	
	private static final int DIALOG_DELETE_MEMO = 1;
	@Override
	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case DIALOG_DELETE_MEMO:
			Builder dialog = new AlertDialog.Builder(this);
			dialog.setTitle(R.string.prompt);
			dialog.setMessage(R.string.delete_memo_prompt);
			dialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener(){
				@Override
				public void onClick(DialogInterface dialog, int which) {
					int _id = mCursor.getInt(mCursor.getColumnIndex(Memo.Columns._ID));
					mMemoCount -= getContentResolver().delete(Memo.Columns.CONTENT_URI, Memo.Columns._ID + "=" + _id, null);					
					displayWhat();
					dialog.dismiss();
				}
			});
			dialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener(){
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}				
			});
			return dialog.create();
		default:
			break;
		}
//		Builder builder = new AlertDialog.Builder(this);
//		View title = LayoutInflater.from(this).inflate(R.layout.dialog_title, null);
//		View view = LayoutInflater.from(this).inflate(R.layout.dialog_view, null);
//		builder.setCustomTitle(title);
//		builder.setView(view);
//		return builder.create();
		return super.onCreateDialog(id);
	}

	private OnClickListener addOnClickListener = new OnClickListener() {	
		public void onClick(View v) {
			Intent intent = new Intent(MemoList.this, AddMemo.class);
			startActivity(intent);
		}
	};
}
